package org.wu.reggie.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wu.reggie.entity.User;
import org.wu.reggie.mapper.UserMapper;
import org.wu.reggie.service.UserService;


@Service
public class UserServiceImpl implements UserService {


    @Autowired(required = false)
    private UserMapper userMapper;

    /**
     * 作用 登录校验
     *
     * @param phone 用户输入的手机号
     * @param code 用户输入的验证码
     * @param redisCode redis缓存的验证码
     * @return
     * */
    @Override
    public User login(String phone, String code, String redisCode) {
        // 1 校验两次验证码是否相同
        User dbUser = null;
        if (redisCode!=null && code.equalsIgnoreCase(redisCode)){
            // 验证码没有问题
            // 2 根据手机号查询用户
            dbUser = userMapper.findByPhone(phone);
            System.out.println("dbUser = " + dbUser);
            if (dbUser==null){
                // 表示是新用户 注册
                 dbUser = new User();
                 dbUser.setPhone(phone);
                 dbUser.setStatus(1);
                 // 这个一定要获取自增的主键
                 userMapper.save(dbUser);
            }
        }
        return dbUser;
    }
}
